﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class CycleCountCriteriaDA : ICycleCountCriteriaDA
    {
        private string CC_CRITERIA_ID = "@CC_CRITERIA_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string DAYS_BETWEEN = "@DAYS_BETWEEN";
        private string INCL_ZERO_COUNT = "@INCL_ZERO_COUNT";
        private string LOC_SELECTION = "@LOC_SELECTION";
        private string MAX_COUNTS = "@MAX_COUNTS";
        private string QTY_LESS_THAN = "@QTY_LESS_THAN";
        private string REQUEST_ONLY = "@REQUEST_ONLY";
        private string SHOW_QTY = "@SHOW_QTY";
        private string SQL_CHECK_CYCLE_COUNT_CRITERIA_ID_UNIQUE = " SELECT COUNT(1) FROM @_@CYCLE_COUNT_CRITERIA WHERE CC_CRITERIA_ID = @CC_CRITERIA_ID";
        private string SQL_CHECK_CYCLE_COUNT_CRITERIA_UNIQUE = " SELECT CC_CRITERIA_ID FROM @_@CYCLE_COUNT_CRITERIA WHERE MAX_COUNTS =@MAX_COUNTS AND SHOW_QTY=@SHOW_QTY AND REQUEST_ONLY = @REQUEST_ONLY AND DAYS_BETWEEN=@DAYS_BETWEEN AND QTY_LESS_THAN = @QTY_LESS_THAN AND  ISNULL(LOC_SELECTION,'')=@LOC_SELECTION AND INCL_ZERO_COUNT = @INCL_ZERO_COUNT";
        private string SQL_DELETE_CYCLE_COUNT_CRITERIA = " DELETE FROM @_@CYCLE_COUNT_CRITERIA WHERE CC_CRITERIA_ID = @CC_CRITERIA_ID ";
        private string SQL_INSERT_CYCLE_COUNT_CRITERIA = " INSERT INTO @_@CYCLE_COUNT_CRITERIA ( WH_ID, CC_CRITERIA_ID, MAX_COUNTS, SHOW_QTY, REQUEST_ONLY, DAYS_BETWEEN, QTY_LESS_THAN, LOC_SELECTION, INCL_ZERO_COUNT, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @CC_CRITERIA_ID, @MAX_COUNTS, @SHOW_QTY, @REQUEST_ONLY, @DAYS_BETWEEN, @QTY_LESS_THAN, @LOC_SELECTION, @INCL_ZERO_COUNT, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_CYCLE_COUNT_CRITERIA = " SELECT  ROWID, WH_ID, CC_CRITERIA_ID, MAX_COUNTS, SHOW_QTY, REQUEST_ONLY, DAYS_BETWEEN, QTY_LESS_THAN, LOC_SELECTION, INCL_ZERO_COUNT, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@CYCLE_COUNT_CRITERIA ";
        private string SQL_UPDATE_CYCLE_COUNT_CRITERIA = " UPDATE @_@CYCLE_COUNT_CRITERIA SET WH_ID = @WH_ID, MAX_COUNTS = @MAX_COUNTS, SHOW_QTY = @SHOW_QTY, REQUEST_ONLY = @REQUEST_ONLY, DAYS_BETWEEN = @DAYS_BETWEEN, QTY_LESS_THAN = @QTY_LESS_THAN, LOC_SELECTION = @LOC_SELECTION, INCL_ZERO_COUNT = @INCL_ZERO_COUNT, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE CC_CRITERIA_ID = @CC_CRITERIA_ID  ";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckCycleCountCriteriaIDUnique(string cycleCountCriteriaID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_CRITERIA_ID", cycleCountCriteriaID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_CYCLE_COUNT_CRITERIA_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckCycleCountCriteriaIDUnique(DataBase dataBase, DbTransaction tran, string cycleCountCriteriaID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_CRITERIA_ID", cycleCountCriteriaID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_CYCLE_COUNT_CRITERIA_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public string CheckCycleCountCriteriaUnique(DataBase dataBase, DbTransaction tran, CycleCountCriteriaInfo cyclecountcriteriaInfo, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@MAX_COUNTS", cyclecountcriteriaInfo.MaxCounts), new SqlParameter("@SHOW_QTY", cyclecountcriteriaInfo.ShowQty), new SqlParameter("@REQUEST_ONLY", cyclecountcriteriaInfo.RequestOnly), new SqlParameter("@DAYS_BETWEEN", cyclecountcriteriaInfo.DaysBetween), new SqlParameter("@QTY_LESS_THAN", cyclecountcriteriaInfo.QtyLessThan), new SqlParameter("@LOC_SELECTION", cyclecountcriteriaInfo.LocSelection), new SqlParameter("@INCL_ZERO_COUNT", cyclecountcriteriaInfo.InclZeroCount) };
            object obj = dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_CYCLE_COUNT_CRITERIA_UNIQUE.Replace("@_@", whLoginID), paras);
            if ((obj != null) && (obj != ""))
            {
                return obj.ToString();
            }
            return "";
        }

        public int DeleteCycleCountCriteria(List<string> cycleCountCriteriaID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < cycleCountCriteriaID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_CRITERIA_ID", cycleCountCriteriaID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteCycleCountCriteria(DataBase dataBase, DbTransaction tran, List<string> cycleCountCriteriaID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < cycleCountCriteriaID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_CRITERIA_ID", cycleCountCriteriaID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllCycleCountCriteria(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllCycleCountCriteria(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public CycleCountCriteriaInfo GetCycleCountCriteriaByID(string cyclecountcriteriaID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID) + " WHERE CC_CRITERIA_ID = @CC_CRITERIA_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_CRITERIA_ID", cyclecountcriteriaID) };
            CycleCountCriteriaInfo cycleCountCriteriaInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    cycleCountCriteriaInfo = new CycleCountCriteriaInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CC_CRITERIA_ID"].ToString(), (reader["MAX_COUNTS"].ToString() != "") ? int.Parse(reader["MAX_COUNTS"].ToString()) : 0, reader["SHOW_QTY"].ToString(), reader["REQUEST_ONLY"].ToString(), (reader["DAYS_BETWEEN"].ToString() != "") ? decimal.Parse(reader["DAYS_BETWEEN"].ToString()) : 0M, (reader["QTY_LESS_THAN"].ToString() != "") ? decimal.Parse(reader["QTY_LESS_THAN"].ToString()) : 0M, reader["LOC_SELECTION"].ToString(), reader["INCL_ZERO_COUNT"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return cycleCountCriteriaInfo;
        }

        public CycleCountCriteriaInfo GetCycleCountCriteriaByID(DataBase dataBase, DbTransaction tran, string cyclecountcriteriaID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID) + " WHERE CC_CRITERIA_ID = @CC_CRITERIA_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CC_CRITERIA_ID", cyclecountcriteriaID) };
            CycleCountCriteriaInfo cycleCountCriteriaInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                cycleCountCriteriaInfo = new CycleCountCriteriaInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CC_CRITERIA_ID"].ToString(), (reader["MAX_COUNTS"].ToString() != "") ? int.Parse(reader["MAX_COUNTS"].ToString()) : 0, reader["SHOW_QTY"].ToString(), reader["REQUEST_ONLY"].ToString(), (reader["DAYS_BETWEEN"].ToString() != "") ? decimal.Parse(reader["DAYS_BETWEEN"].ToString()) : 0M, (reader["QTY_LESS_THAN"].ToString() != "") ? decimal.Parse(reader["QTY_LESS_THAN"].ToString()) : 0M, reader["LOC_SELECTION"].ToString(), reader["INCL_ZERO_COUNT"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return cycleCountCriteriaInfo;
        }

        public DataSet GetCycleCountCriteriaByQueryList(List<string> sqlWhere, CycleCountCriteriaQueryEntity cyclecountcriteriaQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!cyclecountcriteriaQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, cyclecountcriteriaQuery.CurrentPage, cyclecountcriteriaQuery.PageSize, cyclecountcriteriaQuery.SortField, cyclecountcriteriaQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetCycleCountCriteriaByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, CycleCountCriteriaQueryEntity cyclecountcriteriaQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, cyclecountcriteriaQuery.CurrentPage, cyclecountcriteriaQuery.PageSize, cyclecountcriteriaQuery.SortField, cyclecountcriteriaQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertCycleCountCriteria(CycleCountCriteriaInfo cycleCountCriteriaInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                cycleCountCriteriaInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CycleCountCriteria_Parameters(cycleCountCriteriaInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertCycleCountCriteria(DataBase dataBase, DbTransaction tran, CycleCountCriteriaInfo cycleCountCriteriaInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                cycleCountCriteriaInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CycleCountCriteria_Parameters(cycleCountCriteriaInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_CycleCountCriteria_Parameters(CycleCountCriteriaInfo cycleCountCriteriaInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.CC_CRITERIA_ID, SqlDbType.VarChar, 20), new SqlParameter(this.MAX_COUNTS, SqlDbType.Int, 4), new SqlParameter(this.SHOW_QTY, SqlDbType.VarChar, 1), new SqlParameter(this.REQUEST_ONLY, SqlDbType.VarChar, 1), new SqlParameter(this.DAYS_BETWEEN, SqlDbType.Decimal, 13), new SqlParameter(this.QTY_LESS_THAN, SqlDbType.Decimal, 13), new SqlParameter(this.LOC_SELECTION, SqlDbType.VarChar, 20), new SqlParameter(this.INCL_ZERO_COUNT, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(cycleCountCriteriaInfo.WhID))
            {
                paramArray[0].Value = cycleCountCriteriaInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = cycleCountCriteriaInfo.CcCriteriaID;
            paramArray[2].Value = cycleCountCriteriaInfo.MaxCounts;
            paramArray[3].Value = cycleCountCriteriaInfo.ShowQty;
            if (!string.IsNullOrEmpty(cycleCountCriteriaInfo.RequestOnly))
            {
                paramArray[4].Value = cycleCountCriteriaInfo.RequestOnly;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            paramArray[5].Value = cycleCountCriteriaInfo.DaysBetween;
            paramArray[6].Value = cycleCountCriteriaInfo.QtyLessThan;
            if (!string.IsNullOrEmpty(cycleCountCriteriaInfo.LocSelection))
            {
                paramArray[7].Value = cycleCountCriteriaInfo.LocSelection;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            paramArray[8].Value = cycleCountCriteriaInfo.InclZeroCount;
            paramArray[9].Value = cycleCountCriteriaInfo.CreatedBy;
            paramArray[10].Value = cycleCountCriteriaInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateCycleCountCriteria(CycleCountCriteriaInfo cycleCountCriteriaInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                cycleCountCriteriaInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CycleCountCriteria_Parameters(cycleCountCriteriaInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateCycleCountCriteria(DataBase dataBase, DbTransaction tran, CycleCountCriteriaInfo cycleCountCriteriaInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                cycleCountCriteriaInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CycleCountCriteria_Parameters(cycleCountCriteriaInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CYCLE_COUNT_CRITERIA.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

